
# coding: utf-8

# In[1]:

import pandas as pd
import math


# In[4]:

#train_df = pd.read_csv(r"D:\workplace\RSdata\train.csv")
train_df = pd.read_csv(r"C:\Users\CloudCross\workspace\train.csv")


# In[5]:

df = train_df.set_index(['uid','iid'])


# In[6]:

def sim_pearson(prefs,p1,p2):#皮尔森距离
    index1 = prefs.ix[p1].index
    index2 = prefs.ix[p2].index
    si = index1.intersection(index2)
    n = len(si)
    if n==0:
        return 1
    #对所有偏好求和
    sum1=sum([prefs.ix[p1].loc[it,'score'] for it in si])
    sum2=sum([prefs.ix[p2].loc[it,'score'] for it in si])
    
    #求平方和
    sum1Sq=sum([prefs.ix[p1].loc[it,'score']**2 for it in si])
    sum2Sq=sum([prefs.ix[p2].loc[it,'score']**2 for it in si])
    
    #求乘积之和
    pSum=sum([prefs.ix[p1].loc[it,'score']*prefs.ix[p2].loc[it,'score'] for it in si])
    
    #计算皮尔森评价值
    num = pSum - (sum1*sum2/n)
    den = math.sqrt((sum1Sq-(sum1**2)/n)*(sum2Sq-(sum2**2)/n))
    if den==0: return 0
    r = num/den
    return r


# In[81]:

#由对iid1的其他人的评价，以及和p1的相似度，加权给出预测p1对于商品iid1的评分
def get_pre_score(prefs,df,p1,iid1,similarity=sim_pearson):
    pdf = prefs.ix[prefs.iid==iid1,:].ix[:,['uid','score']]
    def f(x):
        return similarity(df,p1,x.loc['uid'])
    sims = pdf.apply(f,axis=1)
    ser1 = sims*pdf['score']
    return sum(ser1[ser1>0])/sum(sims[sims>0])


# In[36]:

test_df = pd.read_csv(r"C:\Users\CloudCross\workspace\test.csv")


# In[79]:

test = test_df.head(10)


# In[84]:
def g(x):
    return get_pre_score(train_df,df,x.loc['uid'],x.loc['iid'],similarity=sim_pearson)

import time
on = time.time()
#get_pre_score(train_df,df,0,12960,similarity=sim_pearson)

res = test.apply(g,axis=1)
print(res)

print(time.time()-on)


# # In[58]:

# pdf = train_df.ix[train_df.iid==12960,:].ix[:,['uid','score']]


# # In[83]:

# test_df.


# # In[72]:

# def f(x):
    # return sim_pearson(df,0,x.loc['uid'])


# # In[73]:

# sims = pdf.apply(f,axis=1)


# # In[71]:

# sim_pearson(df,0,38)


# # In[74]:

# sims


# # In[75]:

# ser1 = sims*pdf['score']


# # In[76]:

# ser1


# # In[77]:

# sum(ser1[ser1>0])/sum(sims[sims>0])


# # In[48]:

# test = test_df.head(50)


# # In[52]:

# test_df


# # In[51]:

# get_pre_score(train_df,df,1,12726,similarity=sim_pearson)


# # In[43]:

# sss = test_df.apply(lambda x:x.loc['uid']+1,axis=1)


# # In[44]:

# sss


# # In[29]:

# pdf = train_df.ix[train_df.iid==0,:].ix[:,['uid','score']]
# def f(x):
    # return sim_pearson(df,1,x)
# pdf['uid'].apply(f)


# # In[28]:

# pdf


# # In[33]:

# s = pdf['uid'].apply(f)*pdf['score']
# s[s>0]


# # In[34]:

# sum(s[s>0])


# # In[30]:

# sim_pearson(df,0,1)


# # In[19]:

# df2 = train_df.set_index(['iid','uid'])


# # In[20]:

# df2.head(100)


# # In[35]:

# df.head(100)
# df1 = df.ix[0]
# index1 = df1.index
# print index1.size
# df2 = df.ix[1]
# index2 = df2.index
# te = index1.intersection(index2)
# print te.size
# l1 = [df1.loc[it,'score']**2 for it in te]
# sumte = sum(l1)
# print len(l1)


# # In[10]:

# import time
# on = time.time()

# print(sim_pearson(df,0,895))

# print(time.time()-on)


# # In[11]:

# train_df.head(10)


# # In[49]:

# df


# # In[14]:

# ser1 = train_df.ix[:,'uid']
# ser2 = train_df.ix[:,'iid']
# uuid = ser1.unique()
# uiid = ser2.unique()
# print(len(uiid))
# print(len(uuid))
# uuid[0:1000]


# # In[61]:

# ser = train_df.ix[:,'uid']
# uuid = ser.unique()
# [(sim_pearson(df,0,it),it) for it in uuid[0:100]]


# # In[ ]:



